<template>
    <div>
        <div style="height: 50px">
            <el-input
                    placeholder="请输入名称"
                    name="username"
                    v-model:value="fileName"
                    suffix-icon="el-icon-user"
                    style="width: 200px;padding-right: 5px;"
            >
            </el-input>
            <el-button class="el_margin_lr" type="primary" icon="el-icon-search" @click="search" >搜索</el-button>

            <el-button class="el_margin_lr" type="warning" icon="el-icon-search" @click="reset" >重置</el-button>
        </div>

        <div style="height: 50px">
            <el-button type="danger" @click="batchDeleteUsers">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>

            <el-upload
                    style="display: inline-flex; margin-left: 5px;margin-right: 5px;"
                    action="http://localhost:8081/file/upload"
                    :show-file-list="false"
                    :on-success="importFiles">
                <el-button size="small" type="primary" style="height: 40px;width: 86px;">导入<i class="el-icon-bottom" style="margin-left: 2px"></i></el-button>
            </el-upload>
        </div>

        <el-table :data="tableData" header-cell-class-name="headerTableBg" @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="140"></el-table-column>
            <el-table-column prop="name" label="文件名" width="140"></el-table-column>
            <el-table-column prop="type" label="文件类型" width="120"></el-table-column>
            <el-table-column prop="size" label="文件大小(kb)" width="200"></el-table-column>
            <el-table-column prop="url"  label="文件url" width="240">
                <template v-slot:default="{ row }">
                    <el-button  type="info" @click="download(row.url)" >下载<i class="el-icon-download" style="margin-left: 2px"></i></el-button>
                </template>
            </el-table-column>
            <el-table-column  label="是否启用">
                <template v-slot:default="{ row }">
                    <el-switch
                            v-model="row.enable"
                            active-color="#13ce66"
                            @change="changeState(row.id)"
                            inactive-color="#ff4949">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="operation" label="操作">
                <template slot-scope="scope">
                    <el-button type="success" @click="dialogFormVisible = true,fileForm=scope.row">编辑<i class="el-icon-edit" style="margin-left: 2px"></i></el-button>

                    <el-popconfirm
                            class="el_margin_lr"
                            confirm-button-text='确定'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            title="确认删除该用户吗？"
                            @confirm="deleteFile(scope.row.id)">
                        <el-button  type="danger" slot="reference">删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>

        </el-table>
        <div >
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageIndex"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalNum">
            </el-pagination>
        </div>

        <el-dialog title="修改文件" style="text-align: center;" :visible.sync="dialogFormVisible" width="30%">
            <el-form label-width=80px >
                <el-form-item label="文件名称" >
                    <el-input v-model="fileForm.name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="updateFile">确 定</el-button>
            </div>
        </el-dialog>


    </div>

</template>

<script>
    import request from "@/utils/request";

    export default {
        name: "File",
        props:{
        },
        data(){
            return{
                tableData:{},
                form:{},
                pageIndex:1,
                totalNum:0,
                pageSize:10,
                multipleSelection:[],
                fileName:"",
                dialogFormVisible:false,
                fileForm:{},
            }
        },
        created() {
            this.load();
            request.get("file/queryAllFilesNum",{
                params:{
                    fileName: this.fileName
                }
            }).then(res=>{
                if (res.code === '200'){
                    this.totalNum=res.data;
                }
            })
        },
        methods:{
            load(){
                request.get("file/queryAllFiles",{
                    params:{
                        pageIndex: this.pageIndex,
                        pageSize: this.pageSize,
                        fileName: this.fileName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.tableData=res.data;
                        this.totalNum=res.data.length;
                    }
                })
            },
            updateFile(){
                request.post("file/editFile",this.fileForm).then(res=>{
                    if (res){
                        this.$message({
                            message: '修改文件成功',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.load();
                    }else{
                        this.$message.error("修改文件失败")
                    }
                })
            },
            search(){
                this.load();
                request.get("file/queryAllFilesNum",{
                    params:{
                        fileName: this.fileName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.totalNum=res.data;
                    }
                })
            },
            changeState(id){
                request.get("file/editFileState",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            download(url){
                window.open(url);
            },
            handleSelectionChange(val){
               this.multipleSelection=val;
            },
            batchDeleteUsers(){
                let ids=this.multipleSelection.map(val =>val.id);
                request.post("file/batchDeleteFile",ids).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            handleCurrentChange(val){
                this.pageIndex=val;
                this.load();
            },
            importFiles(){
                this.$message({
                    showClose: true,
                    message: '导入成功',
                    type: 'success'
                });
                this.load();
            },
            handleSizeChange(val){
                this.pageSize=val;
                this.load();
            },
            deleteFile(id){
                request.get("file/deleteFile",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })

            },
            reset(){
                this.fileName="";
                this.load();
            }
        }
    }
</script>

<style scoped>

</style>